package com.leetcode.no678;

import java.util.Stack;

public class Solution {
    public boolean checkValidString(String s) {
        if (s.length() < 1) {
            return true;
        }

        Stack<Integer> left = new Stack<>();
        Stack<Integer> star = new Stack<>();


        for (int i = 0; i < s.length(); i++) {
            char t = s.charAt(i);

            if (t == '(') {
                left.push(i);
                continue;
            }
            if (t == '*') {
                star.push(i);
                continue;
            }
            if (t == ')') {
                if (!left.isEmpty()) {
                    left.pop();
                    continue;
                }
                if (!star.isEmpty()) {
                    star.pop();
                    continue;
                }
                return false;
            }
        }
        while (!left.isEmpty()) {
            if (star.isEmpty()) {
                return false;
            }
            if (left.peek() < star.peek()) {
                left.pop();
                star.pop();
            } else {
                return false;
            }
        }
        return true;
    }

}
